package com.bwsoft.admin.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.dao.QualityControlDao;
import com.bwsoft.admin.domain.Account;
import com.bwsoft.admin.domain.News;
import com.bwsoft.admin.domain.QualityControl;
import com.bwsoft.admin.domain.QualityControlJoiner;
import com.bwsoft.admin.domain.valueObject.AccountVO;
import com.bwsoft.admin.domain.valueObject.QualityControlJoinerVO;
import com.bwsoft.admin.domain.valueObject.QualityControlVO;
import com.bwsoft.admin.service.QualityControlService;

public class QualityControlAction extends BaseAction_ybd {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7480857504872095247L;
	private static Logger LOG = Logger.getLogger(QualityControlAction.class);
	
	private String gbjgxx;
	private String ptxz;
	private String ptclsj;
	
	public String s_qcId;
	public String s_projectName;
	public String s_sponsor;
	public String s_status;
	public String s_openFlag;
	public String s_ptclsj;
	public String s_ptxz;
	public int joinId;
	
	
	
	public String getS_qcId() {
		return s_qcId;
	}

	public void setS_qcId(String s_qcId) {
		this.s_qcId = s_qcId;
	}

	public String getS_projectName() {
		return s_projectName;
	}

	public void setS_projectName(String s_projectName) {
		this.s_projectName = s_projectName;
	}

	public String getS_sponsor() {
		return s_sponsor;
	}

	public void setS_sponsor(String s_sponsor) {
		this.s_sponsor = s_sponsor;
	}

	public String getS_status() {
		return s_status;
	}

	public void setS_status(String s_status) {
		this.s_status = s_status;
	}

	public String getS_openFlag() {
		return s_openFlag;
	}

	public void setS_openFlag(String s_openFlag) {
		this.s_openFlag = s_openFlag;
	}

	public String getS_ptclsj() {
		return s_ptclsj;
	}

	public void setS_ptclsj(String s_ptclsj) {
		this.s_ptclsj = s_ptclsj;
	}

	public String getS_ptxz() {
		return s_ptxz;
	}

	public void setS_ptxz(String s_ptxz) {
		this.s_ptxz = s_ptxz;
	}

	public String qualityControlIndex(){
		return "qualityControlIndex";
	}
	
	public int getJoinId() {
		return joinId;
	}

	public void setJoinId(int joinId) {
		this.joinId = joinId;
	}

	
	public String getGbjgxx() {
		return gbjgxx;
	}

	public void setGbjgxx(String gbjgxx) {
		this.gbjgxx = gbjgxx;
	}

	public String getPtxz() {
		return ptxz;
	}

	public void setPtxz(String ptxz) {
		this.ptxz = ptxz;
	}

	public String getPtclsj() {
		return ptclsj;
	}

	public void setPtclsj(String ptclsj) {
		this.ptclsj = ptclsj;
	}

	/**
	 * 质量控制查询获取列表
	 */
	public String getQualityControlJson() {

		List<QualityControl> qualityControlSet=null;
			
			//通过字典编号查询字典详情的分页集合
		qualityControlSet = qualityControlService.findData(page, rows, s_qcId,  s_projectName, s_sponsor, s_status, s_openFlag, s_ptclsj, s_ptxz);
			//通过编号查询字典集合的大小	
		Long count = qualityControlService.getTotalCount(s_qcId,  s_projectName, s_sponsor, s_status, s_openFlag, s_ptclsj, s_ptxz);
	
		List<QualityControlVO> listVo = qualityControlService.getVo(qualityControlSet);
		
		responseJson.put("total", count);
		responseJson.put("rows", listVo);

		return "mapJsonResult";
	}
	public String qualityControlEdit(){
		qualityControl = qualityControlService.findDataByID(qualityControl.getQcId());
		return "qualityControlEdit";
	}
	
	/**
	 * 质量控制查询获取列表
	 */
	public String getJoinersJson() {

		int count = 0;
		List<QualityControlJoiner> qualityControlJoinerSet=null;
		
			
			//通过质量控制编号参加人详情的分页集合
		qualityControlJoinerSet = qualityControlService.findJoinersData(page, rows, joinId);
			//通过编号查询参加人集合的大小	
		count = qualityControlService.getJoinersTotalCount(joinId);
		
		List<QualityControlJoinerVO> joinerListVo = qualityControlService.getJoinerVo(qualityControlJoinerSet);
		
		responseJson.put("total", count);
		responseJson.put("rows", joinerListVo);

		return "mapJsonResult";
	}
	
	
	
	public String sponsorInfo(){
		account=qualityControlService.findSponsorDataByID(qualityControl.getSponsor().getAccountId());
		List<Account> list = new ArrayList<Account>();
		list.add(account);
		List<AccountVO> listVO = iAndIService.getVo(list);
		responseJson.put("sponsor", listVO.get(0));
		return "mapJsonResult";
	}
	
	/**
	 * 状态改为关闭
	 * @return
	 */
	public String closeQualityControl(){
		
		msg = qualityControlService.closeQualityControl(qualityControl);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 状态改为开启
	 * @return
	 */
	public String openQualityControl(){
		
		msg = qualityControlService.openQualityControl(qualityControl);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 状态改为关闭
	 * @return
	 */
	public String editQualityControl(){
		qualityControl.setGbjgxx(gbjgxx==null ? false:gbjgxx.equals("1"));
		qualityControl.setPtxz(ptxz==null ? false:ptxz.equals("1"));
		qualityControl.setPtclsj(ptclsj==null ? false:ptclsj.equals("1"));
		msg = qualityControlService.editQualityControl(qualityControl);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String remove(){
		
		msg = qualityControlService.remove(qualityControl);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 上传PDF
	 * @return
	 */
	public String updateFile(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		
		if(pdfFile == null){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "请上传PDF报告！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "请上传PDF报告！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;		
		}
		String fileType = pdfFileFileName.substring(pdfFileFileName.lastIndexOf(".")).toLowerCase();
		if(!".pdf".equals(fileType)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "上传的文件不是PDF类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "上传的文件不是PDF类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		if(!newsService.isFitSize(pdfFile)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "上传的文件不能超过8M！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "上传的文件不能超过8M！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		msg = qualityControlService.updateFile(qualityControl, pdfFile, pdfFileFileName);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
		
		
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
